﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;

using BENBUN.Utils;

namespace BENBUN.UI.Controls
{
    public partial class WIWLogContainer : UserControl
    {
        private string _mode = "all";
        private bool _internet = true;


        public WIWLogContainer()
        {
            InitializeComponent();
        }

        public void loadWIWData()
        {
            toolStripActivitiesMine_Click(null, null);
        }

        public void loadBrowser()
        {
            if (_internet)
            {
                webBrowser1.Visible = true;
                Uri location = new Uri(Settings.Settings.URLBaseWeb + "?token=" + Controller.GetInstance().SessionToken + "&scope=" + _mode);

                webBrowser1.Url = location;
            }
            else
            {
                webBrowser1.Visible = false;
            }
        }

        public void loadData()
        {
            XmlDocument doc = new XmlDocument();
            string apiURL = Settings.Settings.URLBaseAPI + "getData.php";

            apiURL = apiURL + "?token=" + Controller.GetInstance().SessionToken;
            apiURL = apiURL + "&limit=50";
            apiURL = apiURL + "&mode=" + _mode;

            bool otherDataButtons;
            XmlNodeList nodes;
            try
            {
                doc.Load(apiURL);
            }
            catch (Exception)
            {
                _internet = false;
                MessageBox.Show("Communication Failure with WIW Server", "WIW Server Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            try
            {
                nodes = doc.SelectNodes("/wiwdata/wiwentry");

                flowOwn.Visible = false;
                flowOwn.Controls.Clear();

                foreach (XmlNode node in nodes)
                {
                    WIWLogEntryData logdata = new WIWLogEntryData();

                    string tempDate = "", tempTime = "";
                    foreach (XmlNode child in node.ChildNodes)
                    {
                        switch (child.Name)
                        {
                            case "uid":
                                logdata.uid = child.InnerText;
                                break;
                            case "icon":
                                logdata.icon = child.InnerText;
                                break;
                            case "date":
                                tempDate = child.InnerText;
                                break;
                            case "time":
                                tempTime = child.InnerText;
                                break;
                            case "category":
                                logdata.category = child.InnerText;
                                break;
                            case "duration":
                                logdata.duration = int.Parse(child.InnerText);
                                break;
                            case "location":
                                logdata.location = child.InnerText;
                                break;
                            case "climbup":
                                logdata.climbUp = float.Parse(child.InnerText);
                                break;
                            case "climbdown":
                                logdata.climbDown = float.Parse(child.InnerText);
                                break;
                            case "meters":
                                logdata.distance = int.Parse(child.InnerText);
                                break;
                            case "username":
                                logdata.username = child.InnerText;
                                break;
                            default:
                                break;
                        }
                    }

                    logdata.activityDate = DateTime.Parse(tempDate + " " + tempTime);

                    string tempParseDate = tempDate + "T" + tempTime;

                    logdata.activityDate = DateTime.Parse(tempParseDate,  CultureInfo.InvariantCulture);
                    //logdata.activityDate = DateTime.ParseExact(tempParseDate,"YYYY-MM-DDThh:mm:sszzz", CultureInfo.InvariantCulture);

                    if (_mode == "own")
                    {
                        otherDataButtons = true;
                    }
                    else
                    {
                        otherDataButtons = false;
                    }

                    flowOwn.Controls.Add(new WIWLogEntry(logdata, otherDataButtons, splitContainer1.Panel1.Width - 27));
                    _internet = true;
                }
            }
            catch (Exception)
            {
                _internet = false;
                MessageBox.Show("Communication Failure with WIW Server", "WIW Server Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
            flowOwn.Visible = true;
        }

        private void splitContainer1_SizeChanged(object sender, EventArgs e)
        {
            foreach (Control item in flowOwn.Controls)
            {
                item.Width = splitContainer1.Panel1.Width - 27;
            }
        }

        private void toolStripActivitiesAll_Click(object sender, EventArgs e)
        {
            toolStripActivitiesAll.Checked = !toolStripActivitiesAll.Checked;
            toolStripActivitiesMine.Checked = !toolStripActivitiesAll.Checked;
            toolStripActivitiesFriends.Checked = !toolStripActivitiesAll.Checked;
            label1.Text = "All WIW Latest Activites";
            _mode = "all";
            loadBrowser();
            loadData();

        }

        private void toolStripActivitiesMine_Click(object sender, EventArgs e)
        {
            toolStripActivitiesMine.Checked = !toolStripActivitiesMine.Checked;
            toolStripActivitiesFriends.Checked = !toolStripActivitiesMine.Checked;
            toolStripActivitiesAll.Checked = !toolStripActivitiesMine.Checked;
            label1.Text = "My WIW Latest Activites";
            _mode = "own";
            loadBrowser();
            loadData();
        }

        private void toolStripActivitiesFriends_Click(object sender, EventArgs e)
        {
            toolStripActivitiesFriends.Checked = !toolStripActivitiesFriends.Checked;

            toolStripActivitiesMine.Checked = !toolStripActivitiesFriends.Checked;
            toolStripActivitiesAll.Checked = !toolStripActivitiesFriends.Checked;
            label1.Text = "Friends WIW Latest Activites";
            _mode = "friends";
            loadBrowser();
            loadData();
        }

    }
}
